package com.example.basewarehouse;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.basewarehouse.activity.MvcActivity;
import com.example.basewarehouse.activity.MvpActivity;
import com.example.basewarehouse.app.Constants;
import com.example.basewarehouse.bean.CityBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hanlyjiang.library.fileviewer.FileViewer;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.PermissionRequest;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.btn_mvc)
    Button btn_mvc;
    @BindView(R.id.btn_mvp)
    Button btn_mvp;
    @BindView(R.id.btn_pdf)
    Button btn_pdf;

    private static final int RC_WRITE_STOREGE = 1;
    public static String FILE_DIR = Environment.getExternalStorageDirectory() + "/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        requestPermissions();
        initData();
        btn_mvp.setOnClickListener(this);
        btn_mvc.setOnClickListener(this);
        btn_pdf.setOnClickListener(this);
    }

    private void requestPermissions() {
        String[] perms = new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE
        };
        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(
                    new PermissionRequest.Builder(this, RC_WRITE_STOREGE, perms)
                            .setRationale("请求权限")
                            .setPositiveButtonText("确认")
                            .setNegativeButtonText("取消")
                            .build());
        }
    }

    private void initData(){
        StringBuilder stringBuilder = new StringBuilder();
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    getAssets().open("city.json")));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Constants.cityBeanList.addAll(new Gson().fromJson(stringBuilder.toString(),new TypeToken<List<CityBean>>(){}.getType()));
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        if(v==btn_mvc){
            intent = new Intent(MainActivity.this, MvcActivity.class);
            startActivity(intent);
        }else if(v==btn_mvp){
            intent = new Intent(MainActivity.this, MvpActivity.class);
            startActivity(intent);
        }else if(v==btn_pdf){
            String fileName = getFilePath("text.pdf");
            startMuPDFActivityWithExampleFile(fileName);
        }
    }

    private String getFilePath(String fileName) {
        return new File(FILE_DIR + fileName).getAbsolutePath();
    }

    public void startMuPDFActivityWithExampleFile(String fileName) {
        File file = new File(fileName);
        if (!file.isFile()) {
            Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        Uri uri = Uri.fromFile(file);
        FileViewer.startMuPDFActivityByUri(this, uri);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
    @AfterPermissionGranted(RC_WRITE_STOREGE)
    private void methodRequiresTwoPermission() {
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
        if (EasyPermissions.hasPermissions(this, perms)) {
            // Already have permission, do the thing
            // ...
        } else {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(this, "请求权限",
                    RC_WRITE_STOREGE, perms);
        }
    }
}
